package com.zhixi.pattern.important.observerpattern.operation.observable;

import com.zhixi.pattern.important.observerpattern.operation.observer.Observer;

import java.util.ArrayList;
import java.util.List;

/**
 * @作者 ShuHaoNan
 * @邮箱 shuhaonan1@gmail.com
 * @时间 2020/11/18 15:25
 */
public class Weather implements IWeather,Observable {

    private List<Observer> observer = new ArrayList<>();

    /**
     * 添加观察者
     */
    @Override
    public void addObserver(Observer observer) {
        this.observer.add(observer);
    }

    /**
     * 删除观察者
     */
    @Override
    public void deleteObserver(Observer observer) {
        this.observer.remove(observer);
    }

    /**
     * 通知所有观察者
     */
    @Override
    public void notifyObserver(String content) {
        observer.forEach(m -> {
            m.update(content);
        });
    }

    @Override
    public void sunny() {
        String sunny = "晴天";
        System.out.println("当前天气：" + sunny);
        this.notifyObserver(sunny);
    }

    @Override
    public void rain() {
        String rain = "下雨";
        System.out.println("当前天气：" + rain);
        this.notifyObserver(rain);
    }

    @Override
    public void snowy() {
        String snowy = "下雪";
        System.out.println("当前天气：" + snowy);
        this.notifyObserver(snowy);
    }

}
